#include "head.h"

// 顺序表的创建
S_arr_list *S_Arr_Init(void)
{
    S_arr_list *L = (S_arr_list *)malloc(sizeof(S_arr_list));
    if (L == NULL)
    {
        printf("顺序表创建失败\n");
        return NULL;
    }
    memset(L->data, 0, ARR_MAX_SIZ * sizeof(F_data));
    L->len = 0;
    return L;
}

// 顺序表的插入
int S_Arr_insert(S_arr_list *L, int pos, F_data data)
{
    if (L == NULL)
    {
        printf("顺序表 为 NULL\n");
        return -1;
    }
    if (L->len >= ARR_MAX_SIZ)
    {
        printf("顺序表已满\n");
        return -1;
    }
    if (pos < 1 || pos > L->len + 1)
    {
        printf("插入位置不合法\n");
        return -1;
    }

    // 从插入点开始后移数据
    for (int i = L->len; i >= pos; i--)
    {
        L->data[i] = L->data[i - 1];
    }

    L->data[pos - 1] = data;
    L->len++;
    return 0;
}

// 顺序表的删除
S_arr_list *S_Arr_delete(S_arr_list *L, int pos)
{
    if (L == NULL)
    {
        printf("顺序表 为 NULL\n");
        return NULL;
    }
    if (L->len == 0)
    {
        printf("顺序表内无数据\n");
        return NULL;
    }
    if (pos < 1 || pos > L->len)
    {
        printf("删除位置不合法\n");
        return NULL;
    }

    // 保存被删除的数据
    S_arr_list *removed_data = S_Arr_Init(); //顺序表的创建

    // strcpy(removed_data->data, &L->data[pos - 1]);

    *removed_data->data = L->data[pos - 1];
    // 将后续数据前移
    for (int i = pos - 1; i < L->len; i++)
    {
        L->data[i - 1] = L->data[i];
        L->data_1[i - 1] = L->data_1[i];
    }

    L->len--;

    // 释放被删除元素的内存空间
    free(removed_data);
    buy_ticket_arr(L, ARR);

    return L;
}

// 按航班班次删除顺序表节点
void S_Arr_delete_id(S_arr_list *L, int id, U_data log_data, FlightSystem Flight_sys)
{
    int pos = -1;
    U_node *ptr_3 = malloc(sizeof(log_data)); //记录登录账号
    ptr_3->data = log_data;
    if (L == NULL)
    {
        printf("顺序表为NULL\n");
        return;
    }
    if (L->len == 0)
    {
        printf("顺序表内无数据\n");
        return;
    }

    // 查找航班班次为 id 的节点位置
    for (int i = 0; i < L->len; i++)
    {
        // printf("%d\n", L->data[i].id);
        // printf("%s\n", L->data_1[i].name);
        // printf("aaaaaaaaaaaa\n");
        if (L->data[i].id == id && strcmp(L->data_1[i].name, log_data.name) == 0)
        {
            pos = i + 2; // 记录节点位置（从1开始）
            break;
        }
    }
    if (pos == -1) // 如果没找到该节点，输出提示信息并返回一个空结构体
    {
        printf("未找到航班号为%d的节点\n", id);
        return;
    }
    log_data.value = log_data.value + L->data->value;
    int flag = log_data.grade;
    if (flag == 1)
    {
        // S_List_delete_1(Flight_sys.PLATINUM_USER, ptr_3->data);
        S_List_delete_name(Flight_sys.PLATINUM_USER, ptr_3->data.name);
        S_List_insert(Flight_sys.PLATINUM_USER, log_data, 1);

        S_List_File_print(Flight_sys.PLATINUM_USER, PLATINUM_FILE);
    }
    else if (flag == 2)
    {
        // S_List_delete_1(Flight_sys.GOLD_USER, ptr_3->data); //写入金卡用户文件
        S_List_delete_name(Flight_sys.GOLD_USER, ptr_3->data.name);
        S_List_insert(Flight_sys.GOLD_USER, log_data, 1);
        S_List_File_print(Flight_sys.GOLD_USER, GOLD_FILE);
    }
    else if (flag == 3)
    {
        // S_List_delete_1(Flight_sys.SILVER_USER, ptr_3->data); //写入银卡用户文件
        S_List_delete_name(Flight_sys.SILVER_USER, ptr_3->data.name);
        S_List_insert(Flight_sys.SILVER_USER, log_data, 1);
        S_List_File_print(Flight_sys.SILVER_USER, SILVER_FILE);
    }
    else if (flag == 4)
    {
        // S_List_delete_1(Flight_sys.REGULAR_USER, ptr_3->data); //写入普通用户文件
        S_List_delete_name(Flight_sys.REGULAR_USER, ptr_3->data.name);
        S_List_insert(Flight_sys.REGULAR_USER, log_data, 1);
        S_List_File_print(Flight_sys.REGULAR_USER, REGULAR_FILE);
    }

    S_Arr_delete(L, pos);

    return;
}

// 顺序表的遍历
void S_Arr_printf(S_arr_list *L)
{
    if (L == NULL)
    {
        printf("顺序表 为 NULL\n");
        return;
    }
    if (L->len == 0)
    {
        printf("顺序表内无数据\n");
        return;
    }

    for (int i = 0; i < L->len; i++)
    {
        printf("班次: %d 机型: %s 起点站: %s 终点站: %s 起飞时间: %02d:%02d 到达时间: %02d:%02d 飞行时间: %s 票价: %d 载客量: %d 剩余座位: %d 是否飞: %c\n",
               L->data[i].id, L->data[i].flighttype, L->data[i].startcity, L->data[i].arrivecity,
               L->data[i].starttime[0], L->data[i].starttime[1], L->data[i].arrivetime[0], L->data[i].arrivetime[1],
               L->data[i].flytime, L->data[i].value, L->data[i].maxNUM, L->data[i].leftbuyersa, L->data[i].whetherfly);
    }
}

// 购买订单顺序表的遍历
void S_Arr_printf_1(S_arr_list *L, U_data *log_data)
{
    if (L == NULL)
    {
        printf("顺序表 为 NULL\n");
        return;
    }
    if (L->len == 0)
    {
        printf("顺序表内无数据\n");
        return;
    }
    for (size_t i = 0; i < L->len; i++)
    {
        if (strcmp(log_data->name, L->data_1[i].name) == 0)
        // if (log_data.name == L->data_1[i].name && log_data.passwd == L->data_1[i].passwd )
        {
            printf("班次: %d 机型: %s 起点站: %s 终点站: %s 起飞时间: %02d:%02d 到达时间: %02d:%02d 飞行时间: %s 票价: %d 载客量: %d 剩余座位: %d 是否飞: %c\n",
                   L->data[i].id, L->data[i].flighttype, L->data[i].startcity, L->data[i].arrivecity,
                   L->data[i].starttime[0], L->data[i].starttime[1], L->data[i].arrivetime[0], L->data[i].arrivetime[1],
                   L->data[i].flytime, L->data[i].value, L->data[i].maxNUM, L->data[i].leftbuyersa, L->data[i].whetherfly);
        }
    }

    // for (int i = 0; i < L->len; i++)
    // {
    //     printf("班次: %d 机型: %s 起点站: %s 终点站: %s 起飞时间: %02d:%02d 到达时间: %02d:%02d 飞行时间: %s 票价: %d 载客量: %d 剩余座位: %d 是否飞: %c\n",
    //            L->data[i].id, L->data[i].flighttype, L->data[i].startcity, L->data[i].arrivecity,
    //            L->data[i].starttime[0], L->data[i].starttime[1], L->data[i].arrivetime[0], L->data[i].arrivetime[1],
    //            L->data[i].flytime, L->data[i].value, L->data[i].maxNUM, L->data[i].leftbuyersa, L->data[i].whetherfly);
    // }
}

//购票
void buy_ticket(FlightSystem Flight_sys, U_data log_data, int line)
{
    S_arr_list *ptr_1 = S_Arr_Init(); //顺序表的创建

    U_node *ptr_3 = malloc(sizeof(log_data)); //记录登录账号
    ptr_3->data = log_data;

    if (ptr_1 == NULL)
    {
        printf("错误:无法初始化序列列表。\n");
        return;
    }

    F_node *ptr_2 = D_List_find_id(Flight_sys.Flight_list, line); //接收用户选择的航班信息

    if (ptr_2 == NULL)
    {
        printf("错误:找不到给定航班号的航班.\n");
        return;
    }
    *ptr_1[ptr_1->len].data = ptr_2->data;
    // S_Arr_printf(ptr_1);

    // *ptr_1[ptr_1->len].data_1->name = *log_data.name;
    strcpy(ptr_1[ptr_1->len].data_1->name, log_data.name);
    // *ptr_1[ptr_1->len].data_1->passwd = *log_data.passwd;
    strcpy(ptr_1[ptr_1->len].data_1->passwd, log_data.passwd);
    // ptr_1[ptr_1->len].data_1->grade = log_data.grade;
    // strcpy(ptr_1[ptr_1->len].data_1->value, log_data.value);
    ptr_1[ptr_1->len].data_1->value = log_data.value;
    if (ptr_1[ptr_1->len].data_1->value < ptr_2->data.value)
    {
        printf("余额不足\n");
        return;
    }
    log_data.value = log_data.value - ptr_2->data.value;
    int flag = log_data.grade;
    if (flag == 5)
    {
        // S_List_delete_1(Flight_sys.PLATINUM_USER, ptr_3->data);
        S_List_delete_name(Flight_sys.PLATINUM_USER, ptr_3->data.name);
        S_List_insert(Flight_sys.PLATINUM_USER, log_data, 1);
        S_List_File_print(Flight_sys.PLATINUM_USER, PLATINUM_FILE);
    }
    else if (flag == 4)
    {
        // S_List_delete_1(Flight_sys.GOLD_USER, ptr_3->data); //写入金卡用户文件
        S_List_delete_name(Flight_sys.GOLD_USER, ptr_3->data.name);
        S_List_insert(Flight_sys.GOLD_USER, log_data, 1);
        S_List_File_print(Flight_sys.GOLD_USER, GOLD_FILE);
    }
    else if (flag == 3)
    {
        // S_List_delete_1(Flight_sys.SILVER_USER, ptr_3->data); //写入银卡用户文件
        S_List_delete_name(Flight_sys.SILVER_USER, ptr_3->data.name);
        S_List_insert(Flight_sys.SILVER_USER, log_data, 1);
        S_List_File_print(Flight_sys.SILVER_USER, SILVER_FILE);
    }
    else if (flag == 2)
    {
        // S_List_delete_1(Flight_sys.REGULAR_USER, ptr_3->data); //写入普通用户文件
        S_List_delete_name(Flight_sys.REGULAR_USER, ptr_3->data.name);
        S_List_insert(Flight_sys.REGULAR_USER, log_data, 1);
        S_List_File_print(Flight_sys.REGULAR_USER, REGULAR_FILE);
    }

    S_arr_list *L = S_Arr_Init();
    // strcpy(L->data_1->grade, ptr_1[ptr_1->len].data_1->grade);
    L->data_1->grade = ptr_1[ptr_1->len].data_1->grade;
    strcpy(L->data_1->name, ptr_1[ptr_1->len].data_1->name);
    strcpy(L->data_1->passwd, ptr_1[ptr_1->len].data_1->passwd);

    *L->data_1->ticket=  *ptr_1[ptr_1->len].data_1->ticket;
    // strcpy(L->data_1->ticket->arrivecity, ptr_1[ptr_1->len].data_1->ticket->arrivecity);
    // strcpy(L->data_1->ticket, ptr_1[ptr_1->len].data_1->ticket);
    // strcpy(L->data_1->ticket, ptr_1[ptr_1->len].data_1->ticket);
    // strcpy(L->data_1->ticket, ptr_1[ptr_1->len].data_1->ticket);

    printf("%s", L->data_1->ticket->flighttype);
    // strcpy(L->data_1->value, ptr_1[ptr_1->len].data_1->value);
    L->data_1->value = ptr_1[ptr_1->len].data_1->value;
    buy_ticket_arr_1(&ptr_1[ptr_1->len], ARR);
    S_Arr_File_print(ptr_1, ARR);
    printf("您购买的机票:\n");
    S_Arr_printf_1(ptr_1, L->data_1);
    return;
}

//余额充值
void Balance_recharge(FlightSystem Flight_sys, U_data log_data)
{
    int new_value;
    U_node *ptr_3 = malloc(sizeof(log_data)); //记录登录账号
    ptr_3->data = log_data;
    printf("请输入您要充值的金额:");
    scanf("%d", &new_value);
    log_data.value = log_data.value + new_value;
    int flag = log_data.grade;
    if (flag == 1)
    {
        // S_List_delete_1(Flight_sys.PLATINUM_USER, ptr_3->data);
        S_List_delete_name(Flight_sys.PLATINUM_USER, ptr_3->data.name);
        S_List_insert(Flight_sys.PLATINUM_USER, log_data, 1);
        S_List_File_print(Flight_sys.PLATINUM_USER, PLATINUM_FILE);
    }
    else if (flag == 2)
    {
        // S_List_delete_1(Flight_sys.GOLD_USER, ptr_3->data); //写入金卡用户文件
        S_List_delete_name(Flight_sys.GOLD_USER, ptr_3->data.name);
        S_List_insert(Flight_sys.GOLD_USER, log_data, 1);
        S_List_File_print(Flight_sys.GOLD_USER, GOLD_FILE);
    }
    else if (flag == 3)
    {
        // S_List_delete_1(Flight_sys.SILVER_USER, ptr_3->data); //写入银卡用户文件
        S_List_delete_name(Flight_sys.SILVER_USER, ptr_3->data.name);
        S_List_insert(Flight_sys.SILVER_USER, log_data, 1);
        S_List_File_print(Flight_sys.SILVER_USER, SILVER_FILE);
    }
    else if (flag == 4)
    {
        // S_List_delete_1(Flight_sys.REGULAR_USER, ptr_3->data); //写入普通用户文件
        S_List_delete_name(Flight_sys.REGULAR_USER, ptr_3->data.name);
        S_List_insert(Flight_sys.REGULAR_USER, log_data, 1);
        S_List_File_print(Flight_sys.REGULAR_USER, REGULAR_FILE);
    }
}

//退票
void S_Arr_delete_trick(FlightSystem Flight_sys, U_data log_data, S_arr_list *L)
{
    int id;
    S_Arr_File_print_1(&log_data);
    printf("请输入要删除的航班班次:");
    scanf("%d", &id);
    S_Arr_delete_id(L, id, log_data, Flight_sys);
}

//打印已购票信息
void S_Arr_File_print_1(U_data *log_data)
{
    S_arr_list *ptr_1_1 = S_Arr_Init(); //顺序表的创建
    S_Arr_File_print(ptr_1_1, ARR);
    printf("您购买的机票:\n");
    S_Arr_printf_1(ptr_1_1, log_data);
    return;
}

// 从文件中读取信息到顺序表
void S_Arr_File_print(S_arr_list *list, const char *filename)
{
    FILE *file = fopen(filename, "r");
    if (file == NULL)
    {
        printf("无法打开文件");
        return;
    }

    int id, grade, value, maxNUM, leftbuyersa;
    char name[50], passwd[50], flighttype[50], startcity[50], arrivecity[50], flytime[50];
    int starttime[2], arrivetime[2];
    char whetherfly;

    while (fscanf(file, "%d\t%s\t%s\t%d\t%d\t%s\t%s\t%s\t%d:%d\t%d:%d\t%s\t%d\t%d\t%d\t%c\n",
                  &id, name, passwd, &grade, &value, flighttype, startcity, arrivecity,
                  &starttime[0], &starttime[1], &arrivetime[0], &arrivetime[1], flytime,
                  &value, &maxNUM, &leftbuyersa, &whetherfly) != EOF)
    {

        list->data[list->len].id = id;
        strcpy(list->data_1[list->len].name, name);
        strcpy(list->data_1[list->len].passwd, passwd);
        list->data_1[list->len].grade = grade;
        list->data_1[list->len].value = value;
        strcpy(list->data[list->len].flighttype, flighttype);
        strcpy(list->data[list->len].startcity, startcity);
        strcpy(list->data[list->len].arrivecity, arrivecity);
        list->data[list->len].starttime[0] = starttime[0];
        list->data[list->len].starttime[1] = starttime[1];
        list->data[list->len].arrivetime[0] = arrivetime[0];
        list->data[list->len].arrivetime[1] = arrivetime[1];
        strcpy(list->data[list->len].flytime, flytime);
        list->data[list->len].value = value;
        list->data[list->len].maxNUM = maxNUM;
        list->data[list->len].leftbuyersa = leftbuyersa;
        list->data[list->len].whetherfly = whetherfly;
        list->len++;
    }

    fclose(file);
    return;
}

// 将顺序表的信息写入文件
void buy_ticket_arr(S_arr_list *list, const char *filename)
{
    FILE *file = fopen(filename, "w");
    if (file == NULL)
    {
        printf("无法打开文件");
        return;
    }
    for (size_t i = 0; i < list->len; i++)
    {
        fprintf(file, "%d\t", list->data[i].id);
        fprintf(file, "%s\t", list->data_1[i].name);
        fprintf(file, "%s\t", list->data_1[i].passwd);
        fprintf(file, "%d\t", list->data_1[i].grade);
        fprintf(file, "%d\t", list->data_1[i].value);
        fprintf(file, "%s\t", list->data[i].flighttype);
        fprintf(file, "%s\t", list->data[i].startcity);
        fprintf(file, "%s\t", list->data[i].arrivecity);
        fprintf(file, "%d:%d\t", list->data[i].starttime[0], list->data[i].starttime[1]);
        fprintf(file, "%d:%d\t", list->data[i].arrivetime[0], list->data[i].arrivetime[1]);
        fprintf(file, "%s\t", list->data[i].flytime);
        fprintf(file, "%d\t", list->data[i].value);
        fprintf(file, "%d\t", list->data[i].maxNUM);
        fprintf(file, "%d\t", list->data[i].leftbuyersa);
        fprintf(file, "%c\n", list->data[i].whetherfly);
    }

    fclose(file);
}

// 将顺序表的信息写入文件
void buy_ticket_arr_1(S_arr_list *list, const char *filename)
{
    FILE *file = fopen(filename, "a");
    if (file == NULL)
    {
        printf("无法打开文件");
        return;
    }

    fprintf(file, "%d\t", list->data->id);
    fprintf(file, "%s\t", list->data_1->name);
    fprintf(file, "%s\t", list->data_1->passwd);
    fprintf(file, "%d\t", list->data_1->grade);
    fprintf(file, "%d\t", list->data_1->value);
    fprintf(file, "%s\t", list->data->flighttype);
    fprintf(file, "%s\t", list->data->startcity);
    fprintf(file, "%s\t", list->data->arrivecity);
    fprintf(file, "%d:%d\t", list->data->starttime[0], list->data->starttime[1]);
    fprintf(file, "%d:%d\t", list->data->arrivetime[0], list->data->arrivetime[1]);
    fprintf(file, "%s\t", list->data->flytime);
    fprintf(file, "%d\t", list->data->value);
    fprintf(file, "%d\t", list->data->maxNUM);
    fprintf(file, "%d\t", list->data->leftbuyersa);
    fprintf(file, "%c\n", list->data->whetherfly);

    fclose(file);
}
